// pages/login/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: [],
    showPhone: false,
    showUserInfoBtn: false,
    openID: '',
    phone: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.checkToken();
    this.getUserInfo();
  },
  // 检查token
  checkToken() {
    wx.getStorage({
      key: 'token',
      success: function(res) {
        getApp().data.token = res.data
        wx.redirectTo({
          url: '/pages/index/index',
        });
      },
      fail: () => {
        this.wxLogin();
      }
    })
  },
  // 微信登录
  wxLogin() {
    const app = getApp();
    wx.showLoading({
      title: '正在登录...',
    });
    wx.login({
      success: res => {
        wx.request({
          url: app.data.baseUrl + 'wx-login',
          method: 'POST',
          data: {code: res.code},
          success: res => {
            if (res.statusCode === 200) {
              if (res.data.data.token) {
                wx.setStorage({
                  key: 'token',
                  data: res.data.data.token,
                });
                getApp().data.token = res.data.data.token;
                wx.redirectTo({
                  url: '/pages/index/index',
                })
              } else {
                this.setData({
                  showPhone: true,
                  openID: res.data.data.openID
                })
              }
            } else {
              wx.showToast({
                title: res.data.errors[0]['message'],
                icon: 'none'
              })
            }
          },
          complete: () => {
            wx.hideLoading();
          }
        })
      }
    })
  },
  // get user info
  getUserInfo() {
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: res => {
              this.setData({
                userInfo: res.userInfo
              })
            }
          });
        } else {
          this.setData({
            showUserInfoBtn: true
          })
        }
      }
    }) 
  },
  // get user info by btn
  getUserInfoByBtn(e) {
    this.setData({
      userInfo: e.detail.userInfo,
      showUserInfoBtn: false
    })
  },
  // phone input change
  phoneChange(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  // bind btn click
  bindBtnClick () {
    const app = getApp();
    if (this.data.phone === '') {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      });
      return false;
    };
    wx.showLoading({
      title: '正在绑定账号...',
    });
    wx.request({
      url: app.data.baseUrl + 'bind',
      method: 'POST',
      data: {
        phone: this.data.phone,
        openID: this.data.openID,
        user: this.data.userInfo
      },
      success: res => {
        if (res.statusCode === 200) {
          wx.setStorage({
            key: 'token',
            data: res.data.data.token,
          });
          getApp().data.token = res.data.data.token;
          wx.redirectTo({
            url: '/pages/index/index',
          })
        } else {
          wx.showToast({
            title: res.data.errors[0]['message'],
            icon: 'none'
          })
        }
      },
      complete: () => {
        wx.hideLoading();
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})